package com.czh.servlet.Bill;

import com.czh.pojo.Bill;
import com.czh.pojo.Provider;
import com.czh.service.Bill.BillServiceImpl;
import com.czh.service.Provider.ProviderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)   {
        String method = req.getParameter("method");
        if (method.equals("query")){
            getBillList(req,resp);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
    protected void getBillList(HttpServletRequest req, HttpServletResponse resp)   {
        String queryProductName = req.getParameter("queryProductName");
        String queryProviderId = req.getParameter("queryProviderId");
        String queryIsPayment = req.getParameter("queryIsPayment");

        BillServiceImpl billService = new BillServiceImpl();
        if (queryProviderId == null){
            queryProviderId = String.valueOf(0);
        }
        if (queryIsPayment == null){
            queryIsPayment = String.valueOf(0);
        }
        List<Provider> providerList = new ProviderServiceImpl().getProviderList(null, null);
        List<Bill> billList = billService.BillList(queryProductName, Integer.parseInt(queryProviderId), Integer.parseInt(queryIsPayment));
        req.setAttribute("billList",billList);
        req.setAttribute("providerList",providerList);
        req.setAttribute("queryProviderId",queryProviderId);
        req.setAttribute("queryProductName",queryProductName);
        req.setAttribute("queryIsPayment",queryIsPayment);
        try {
            req.getRequestDispatcher("billlist.jsp").forward(req,resp);
        }catch (ServletException | IOException e){
            throw new RuntimeException("getBillList异常！");
        }
    }
}
